package fr.gdi.android.news.importer;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import fr.gdi.android.news.FeedDescription;
import fr.gdi.android.news.R;
import fr.gdi.android.news.utils.FeedUtils;
import fr.gdi.android.news.utils.PreferenceUtils;
import fr.gdi.android.news.utils.Utils;

public abstract class AbstractFeedImporterTask extends  AsyncTask<Void, Void, ImportResult>
{

    private ProgressDialog dialog;

    private Activity context;
    protected File file;
    private Runnable callback;
    
    public AbstractFeedImporterTask(Activity context, File file, Runnable callback)
    {
        this.context = context;
        this.file = file;
        this.callback = callback;
    }

    @Override
    protected void onPreExecute()
    {
        dialog = ProgressDialog.show(context, 
                context.getText(R.string.import_wait_title), 
                context.getText(R.string.import_wait_message), 
                true,  
                false);
    }
    
    @Override
    //@TODO i18n
    protected void onPostExecute(ImportResult result)
    {
        dialog.dismiss();
        if (result.feedCount == 0 )
        {
            Utils.showWarning(context, String.format(context.getText(R.string.import_no_feed_found).toString(), file.getName()));
        }
        else
        {
            if ( result.conflicts == 0 ) 
            {
                Utils.showSuccess(context, String.format(context.getText(R.string.feed_import_success).toString(), result.feedCount));
            }
            else 
            {
                Utils.showWarning(context, String.format(context.getText(R.string.feed_import_conflict).toString(), (result.feedCount - result.conflicts), result.conflicts));
                         
            }
        }
        
        if ( callback != null ) callback.run();
    }
    
    @Override
    protected ImportResult doInBackground(Void... args)
    {
        try
        {
            final List<FeedDescription> feeds = getFeeds();
            return doImport(context, feeds);
        }
        catch (Exception e)
        {
            return new ImportResult(e);
        }
    }

    protected abstract List<FeedDescription> getFeeds() throws Exception;
    
    protected ImportResult doImport(Context context, final List<FeedDescription> feeds)
    {
        List<FeedDescription> declaredFeeds = FeedUtils.getFeedDescriptions(context, null);
        
        int conflicts = 0;
        for (FeedDescription i : feeds)
        {
            String name = i.name.replaceAll(":",  " ").replace(", ", " ");
            boolean feedExists = false;
            for (FeedDescription d : declaredFeeds)
            {
                if (TextUtils.equals(name, d.name))
                {
                    conflicts++;
                    feedExists = true;
                }
            }
            if (!feedExists)
            {
                FeedUtils.addFeed(context, name, i.url);
                
                if ( !TextUtils.isEmpty(i.source) ) 
                {
                    String pref = FeedUtils.getPackagePrefKey(i.source);
                    PreferenceUtils.addStringPreference(context, pref, i.url);
                }
            }
        }
        
        return new ImportResult(feeds.size(), conflicts);
    }
    
}